package com.xxxx.server.exception;

import com.xxxx.server.pojo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author 无恙
 * @date 2020/9/8
 */
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(SQLException.class)
     public RespBean mySqlException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联数据，操作失败!");
        }
        return RespBean.error("数据库异常，操作失败！");
    }

    /**
     * 捕捉参数异常
     * @param e
     * @return
     */
    @ExceptionHandler(ParamsException.class)
    public RespBean userDisabledException(ParamsException e){
        //打印异常信息在控制台
        System.out.println(e.getMsg());
        return RespBean.error(e.getMsg());
    }
}
